﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Text;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;


namespace Karasoft.Web.UI
{
    [DefaultProperty("Text")]
    [ToolboxData("<{0}:HijriWebCalendar runat=server></{0}:HijriWebCalendar>")]
    public class HijriWebCalendar : Calendar
    {
       
      //  override 

        private DateTime FirstCalendarDay(DateTime visibleDate)
        {
            DateTime date = visibleDate;
            if (this.IsMinSupportedYearMonth(date))
            {
                return date;
            }
            int num = ((int)this.threadCalendar.GetDayOfWeek(date)) - this.NumericFirstDayOfWeek();
            if (num <= 0)
            {
                num += 7;
            }
            return this.threadCalendar.AddDays(date, -num);
        }


        private bool IsMinSupportedYearMonth(DateTime date)
        {
            return this.IsTheSameYearMonth(this.minSupportedDate, date);
        }

        private bool IsTheSameYearMonth(DateTime date1, DateTime date2)
        {
            return (((this.threadCalendar.GetEra(date1) == this.threadCalendar.GetEra(date2)) && (this.threadCalendar.GetYear(date1) == this.threadCalendar.GetYear(date2))) && (this.threadCalendar.GetMonth(date1) == this.threadCalendar.GetMonth(date2)));
        }

        private DateTime EffectiveVisibleDate()
        {
            DateTime visibleDate = this.VisibleDate;
            if (visibleDate.Equals(DateTime.MinValue))
            {
                visibleDate = this.TodaysDate;
            }
            if (this.IsMinSupportedYearMonth(visibleDate))
            {
                return this.minSupportedDate;
            }
            return this.threadCalendar.AddDays(visibleDate, -(this.threadCalendar.GetDayOfMonth(visibleDate) - 1));
        }


        private int NumericFirstDayOfWeek()
        {
            if (this.FirstDayOfWeek != FirstDayOfWeek.Default)
            {
                return (int)this.FirstDayOfWeek;
            }
            return (int)System.Globalization.DateTimeFormatInfo.CurrentInfo.FirstDayOfWeek;
        }

 



        private System.Globalization.Calendar threadCalendar { get; set; }


        public DateTime minSupportedDate { get; set; }

        public FirstDayOfWeek FirstDayOfWeek { get; set; }
    }
    public enum FirstDayOfWeek
    {
        Sunday,
        Monday,
        Tuesday,
        Wednesday,
        Thursday,
        Friday,
        Saturday,
        Default
    }

 

}
